"use strict";

function foo() {
    var x = "Hello, " + y;
    console.log(x);
    var y = "Bob";
}

foo()
// Hello, undefined

function foo() {
    var y;
    var x = "Hello, " + y;
    console.log(x);
    y = "Bob";
}

function foo() {
    // var 可以换行，不会被自动加分号
    var
        x = 1, 
        y = x + 1, 
        z, i;
    // ...
}

var course = "Learn JavaScript";
alert(course);          // "Learn JavaScript"
alert(window.course);   // "Learn JavaScript"

function foo() {
    alert('foo');
}

foo();          // 直接调用foo()
window.foo();   // 通过window.foo()调用


// 唯一的全局变量
var MYAPP = {};

MYAPP.name = "myapp";
MYAPP.version = 1.0;

MYAPP.foo = function () {
    return "foo";
}

function foo() {
    for (var i = 0; i < 100; i++) {
        var test = i;
    }

    return [i, test];
}

foo();  // [100, 99]
// 仍然可以引用变量 i

function foo() {
    var sum = 0;
    for (let i = 0; i < 100; i++) {
        sum += i;
    }

    console.log(sum);
    console.log(i);
}

foo();
// Uncaught ReferenceError: i is not defined

const PI = 3.14;
PI = 3;
// Uncaught TypeError: Assignment to constant variable.

var arr = ["Hello", "JavaScript", "ES6"];
var x = arr[0];
var y = arr[1];
var z = arr[2];

var [x, y, z] = ["Hello", "JavaScript", "ES6"];
console.log("x: " + x + ", y: " + y + ", z: " + z);
// x: Hello, y: JavaScript, z: ES6

{
    let [x, [y, z]] = ["Hello", ["JavaScript", "ES6"]];
    console.log("x: " + x + ", y: " + y + ", z: " + z);
    // x: Hello, y: JavaScript, z: ES6
}

{
    let [, , z] = ["Hello", "JavaScript", "ES6"];
    console.log("z: " + z);
    // z: ES6
}

{
    let person = {
        name: "Lucca",
        age: 20,
        gender: "male",
        passport: "Forgotten",
        school: "BIT"
    };
    let {name, age, passport} = person;
    console.log('name = ' + name + ', age = ' + age + ', passport = ' + passport);
    // name = Lucca, age = 20, passport = Forgotten
}

{
    let person = {
        name: "Lucca",
        age: 20,
        gender: "male",
        passport: "Forgotten",
        school: "BIT",
        address: {
            city: "Beijing",
            street: "Gongchen Street",
            zipcode: "Unknown"
        }
    };
    let { name, address: {city, zip} } = person;
    console.log('name = ' + name + ', city = ' + city);
    // name = Lucca, city = Beijing
    console.log('zip = ' + zip);
    // zip = undefined
    console.log('address = ' + address);
    // Uncaught ReferenceError: address is not defined
}

{
    let person = {
        name: "Lucca",
        age: 20,
        gender: "male",
        passport: "Forgotten",
        school: "BIT",
        address: {
            city: "Beijing",
            street: "Gongchen Street",
            zipcode: "Unknown"
        }
    };
    let { name: nm, address: ad } = person;
    console.log('name = ' + nm);
    // name = Lucca
    ad;
    // < {city: "Beijing", street: "Gongchen Street", zipcode: "Unknown"}
}

{
    let person = {
        name: "Lucca",
        age: 20,
        gender: "male",
        passport: "Forgotten",
        school: "BIT"
    };
    let { name, age=10, single=true } = person;
    console.log("name = " + name + ", age = " + age + ", single = " + single);
    // name = Lucca, age = 20, single = true
}

var x = 1, y = 2;
[x, y] = [y, x];

var { hostname: domain, pathname: path } = location;

function buildDate({year, month, day, hour=0, minute=0, second=0}) {
    return new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second);
}

buildDate({year: 2021, month: 4, day: 27});
// Tue Apr 27 2021 00:00:00 GMT+0800 (中国标准时间)

buildDate({year: 2021, month: 4, day: 27, hour: 13, minute: 22, second: 11});
// Tue Apr 27 2021 13:22:11 GMT+0800 (中国标准时间)